//index.js
//获取应用实例
const app = getApp()
const util = require('../../utils/util.js')

Page({
  data: {
		state: 0,
		imgHeight:'980rpx',
		imgWidth:'540rpx',
		setTime: '',
    audioPath: '',
    ani:'',
    productUp: {},
    bottle: {},
    showShelter: false,
    day:null,
    month:null,
    scrollHeight:null,
    water: {},
    showWater: false,
    waterBottom: '',
  },
  onLoad: function () {
    // 获取缓存的打开小程序时间，以及点击花的次数
    // 并通过获取到的缓存信息，判断是否重置花的浇灌次数和花现在的展示状态


    // 处理初加载时状态码超出的异常情况
    // let getStorageState = 0
    // if (Number(wx.getStorageSync('state')) >= 4){
    //   getStorageState = 3
    // }else{
    //   getStorageState = Number(wx.getStorageSync('state'))
    // }
    // this.setData({
    //   day: (wx.getStorageSync('logs') || []).map(log => {
    //     return util.getDay(new Date(log))
    //   }),
    //   month: (wx.getStorageSync('logs') || []).map(log => {
    //     return util.getMonth(new Date(log))
    //   }),
    //   state: getStorageState// 获取缓存中的浇灌次数
    // })
    // 允许缓存5000条日志
    // if (this.data.day.length >= 5000){
    //   wx.removeStorageSync('logs')
    // }
    // 重置开花状态
    // if (this.data.day.length >= 2){
    //   // console.log("day", this.data.day)
    //   let monthTwo = this.data.month[1]
    //   let monthOne = this.data.month[0]
    //   let dayTwo = this.data.day[1]
    //   let dayOne = this.data.day[0]
    //   // console.log("月", monthTwo, monthOne, "日", dayTwo, dayOne)
    //   // 每天24点重置浇灌次数,（月数天数不一致改变）
    //   if (monthTwo === monthOne){
    //     if (dayTwo !== dayOne){
    //       this.setData({
    //         state: 0
    //       })
    //     }
    //   }else{
    //     this.setData({
    //       state: 0
    //     })
    //   }
    // }
  },
  /**
   * 音频播放器
   */
  playAudio(path){
    
    //context.src = '../src/audio/2817.mp3';
    const innerAudioContext = wx.createInnerAudioContext()
    innerAudioContext.src = path;
    innerAudioContext.play();
    innerAudioContext.onPlay(() => {
      // console.log('开始播放')
    })
    innerAudioContext.onError((res) => {
      console.log(res.errMsg)
      console.log(res.errCode)
    })
  },
  /**
   * 人物显现动画效果
   */
  translateImg(o,x){
    var animation = wx.createAnimation({
      duration: 1000,
      timingFunction: 'ease',
      delay: 100,
    });
    animation.opacity(o).translate(x, 0).step()
    this.setData({
      ani: animation.export()
    })
  },
  /**
   * 清除遮挡层
   */
  colseShelter(){
    // this.translateImg(300)
    let that = this
    that.translateImg(0, 0)
    setTimeout(function () {
      that.setData({
        showShelter: false,
        state: 3,
      });
    }, 800)
    // this.onWater(0,0)
    // wx.setStorageSync('state', 3)
  },

  /**
   * 左下产品动画效果
   */
  onShow: function () {
    var animation = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease',
    })
    this.animation = animation
    var next = true;
    //连续动画关键步骤
    setInterval(function () {
      if (next) {
        this.animation.scale(0.95).step()
        next = !next;
      } else {
        this.animation.scale(1).step()
        next = !next;
      }
      this.setData({
        productUp: animation.export()
      })
    }.bind(this), 500)
  },
  /**
   * 点击开花，主要事件
   */
  bloom(){
    var that = this
    let person = null;
    let height = '1000rpx';
    let width = '570rpx';
    this.data.state += 1;
    // wx.setStorageSync('state', this.data.state)
    if (this.data.state == 4) {
      that.setData({
        showShelter: true,
      })
      // this.translateImg(0)
      setTimeout(function(){
        that.translateImg(1, 0)
      },100)
      this.playAudio('https://gitee.com/foc/maintenance_projects/raw/master/pages/src/audio/3.mp3')
      // wx.setStorageSync('state', 4)
    }
    person = this.data.state
    this.setData({
      imgHeight: height,
      imgWidth: width,
    });
    //点击反馈
    setTimeout(function () {
      that.setData({
        imgHeight: '980rpx',
        imgWidth: '540rpx',
      })
    }, 100);
    this.getWaterBottom()
    //开花延迟,状态4不延迟
    if(this.data.state < 4){
      setTimeout(function () {
        that.setData({
          state: person,
        })
      }, 3000);
    }else{
      that.setData({
        state: person,
      })
    }
    // 处理药瓶浇灌动画
    if (this.data.state < 4){
      setTimeout(function () {
        that.playAudio('https://gitee.com/foc/maintenance_projects/raw/master/pages/src/audio/52.mp3');
      }, 2000)
      // 像素单位转化
      var systemInfo = wx.getSystemInfoSync();
      let y = null;
      if(this.data.state == 1){
        y = -600
      }else if(this.data.state == 2){
        y = -680
      }else if(this.data.state == 3){
        y = -860
      }
      let conversionX = -200 / 750 * systemInfo.windowWidth
      let conversionY = y / 750 * systemInfo.windowWidth
      this.onWater(conversionX, conversionY, -135)
      this.showWaterHandle()
      setTimeout(function () {
        // that.onWater(conversionX + 100, conversionY, 0);
        that.onWater(0, 0)
      }, 2500);
    }
    // console.log("state",this.data.state)
  },
  //药瓶浇灌动画
  onWater(x,y,r){
    var animationTwo = wx.createAnimation({
      duration: 1000,
      timingFunction: 'ease',
      delay: 100,
    })
    this.animationTwo = animationTwo
    animationTwo.opacity(1).translate(x, y).step()
    animationTwo.rotate(r).step();
    this.setData({
      bottle: animationTwo.export()
    })
  },
  showWaterHandle(){
    let that = this
    setTimeout(function () {
      that.setData({
        showWater: true
      })
    }, 2000);
    setTimeout(function (){
      that.drippingWater(20)
    },2100)
    setTimeout(function () {
      that.setData({
        showWater: false
      })
      that.drippingWater(0)
    }, 3000)
  },
  /**
   * 浇灌的水滴动画
   */
  drippingWater(y){
    var animationThree = wx.createAnimation({
      duration: 1000,
      timingFunction: 'ease',
      delay: 100,
    })
    this.animationThree = animationThree
    animationThree.opacity(1).translate(0, y).step()
    this.setData({
      water: animationThree.export()
    })
  },
  /**
   * 根据花的状态调整水滴的高度
   */
  getWaterBottom(){
    let bottom = null;
    if (this.data.state == 1){
      bottom = "700rpx"
    }else if(this.data.state == 2){
      bottom = "780rpx"
    } else if (this.data.state == 3) {
      bottom = "960rpx"
    }
    this.setData({
      waterBottom: bottom
    })
  },
  /**
   * 跳转到产品介绍
   */
  goProduct(){
    wx.navigateTo({
      url: '../product/product',
    })
  },

  /**
   * 计算时间差
   */
  shijiancha: function (faultDate, completeTime) {
    var stime = Date.parse(new Date(faultDate));
    var etime = Date.parse(new Date(completeTime));
    var usedTime = etime - stime; //两个时间戳相差的毫秒数
    var days = Math.floor(usedTime / (24 * 3600 * 1000));
    //计算出小时数
    var leave1 = usedTime % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
    var hours = Math.floor(leave1 / (3600 * 1000));
    return hours;
  },
})